import { ProjectListApi } from '@/services/ProjectCenter'
export const operaProjectMixin = {
  props: {
    objRowRequest: Object,
    depArr: {
      type: Array,
      default: () => {
        return []
      }
    }
  },
  data () {
    var checkProName = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入项目名称'))
      } else if ((/^[\s　]|[ ]$/gi).test(value)) {
        callback(new Error('项目名称首尾不能含有空格！'))
      } else {
        callback()
      }
    }
    var checkProNickName = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入项目简称'))
      } else if ((/^[\s　]|[ ]$/gi).test(value)) {
        callback(new Error('项目简称首尾不能含有空格！'))
      } else {
        callback()
      }
    }
    // 验证身份证
    var checkCreditCode = (rule, value, callback) => {
      if (value === '') {
        callback()
      } else if (!/^[1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/.test(value)) {
        callback(new Error('请输入正确的身份证'))
      } else {
        callback()
      }
    }
    return {
      status: '',//状态
      isSyncProjectId: '',
      isSync: 0,//0:不同步  1：同步
      qrCode: '', // 二维码
      selectedNode: {},
      organizationDialogVisible: false,
      deptArr: [], // 存放部门数组
      selectLeaderNode: {}, // 项目经理对象
      selectLeaderDialogVisible: false,
      tableRowData: {},
      managerArr: [],
      // 获取人员列表请求参数
      getJobUserListParam: {
        org_no: '',
        dept_no: '',
        job_id: '',
        node_type: '',
        user_name: '',
        is_page: 1,
        page: 1,
        page_size: 10
      },
      // 根据业务身份和专业类型动态获取需要渲染的表单
      dynamicForm: [],
      rules: {
        project_code: [
          { required: true, message: '项目代码不能为空', trigger: 'blur' }
        ],
        is_indust_project: [
          { required: true, message: '请选择是否工业化项目', trigger: 'blur' }
        ],
        project_name: [
          { required: true, validator: checkProName, trigger: 'blur' }
        ],
        nick_name: [
          { required: true, validator: checkProNickName, trigger: 'blur' }
        ],
        business_type: [
          { required: true, message: '请选择业务身份', trigger: 'change' }
        ],

        major_type: [
          { required: true, message: '请选择专业类型', trigger: 'change' }
        ],
        building_type: [
          { required: true, message: '请选择建筑类型', trigger: 'change' }
        ],
        floorage: [
          { required: true, message: '请输入建筑面积', trigger: 'blur' }
        ],
        address: [
          { required: true, message: '请输入详细地址', trigger: 'blur' }
        ],
        license_number: [
          { required: true, message: '请输入施工许可证编号', trigger: 'blur' }
        ],
        assembly_rate: [
          { required: true, message: '请输入装配率', trigger: 'blur' }
        ],
        status: [
          { required: true, message: '请选择项目状态', trigger: 'blur' }
        ],
        contract_total: [
          { required: true, message: '请输入合同总额', trigger: 'blur' }
        ],
        sign_time: [
          { required: true, message: '请选择签订时间', trigger: 'blur' }
        ],
        application: [
          { required: true, message: '请输入技术应用', trigger: 'blur' }
        ],
        lonlat: [
          { required: true, message: '请选择地图位置', trigger: 'change' }
        ],
        leader_name: [
          { required: true, message: '请输入项目经理', trigger: 'change' }
        ],
        dept_no: [
          { required: true, message: '请选择项目部', trigger: 'change' }
        ],
        city_no: [
          { required: true, message: '请选择项目地址', trigger: 'change' }
        ],
        construct_nature: [{ required: true, message: '请选择建设性质', trigger: 'change' }],
        project_type: [{ required: true, message: '请选择项目类型', trigger: ['blur', 'change'] }],
        project_usage: [
          { required: true, message: '请选择工程用途', trigger: ['blur', 'change'] }
        ],
        project_status: [{ required: true, message: '请选择项目状态', trigger: ['blur', 'change'] }],
        invest_nature: [{ required: true, message: '请选择项目投资性质', trigger: ['blur', 'change'] }],
        total_build_area: [{ required: true, message: '请输入总建筑面积', trigger: 'blur' }],
        org_type: [{ required: true, message: '请选择组织方式', trigger: ['blur', 'change'] }],
        struct_type: [{ required: true, message: '请选择结构类型', trigger: ['blur', 'change'] }],
        is_wisdom: [{ required: true, message: '请选择智慧工地项目', trigger: ['blur', 'change'] }],
        is_use_bim: [{ required: true, message: '请选择BIM技术', trigger: ['blur', 'change'] }],
      },
      //业务身份标准码列表
      businessTypeList: [],
      // 建筑类型标准码列表
      buildingTypeList: [],
      // 专业类型标准码列表
      majorTypeList: [],
      // 项目状态标准码列表
      projectStatusList: [
        { name: '未完成', value: '1' },
        { name: '已完成', value: '2' }
      ],
      buildUnitArr: [
        {
          build_unit: ''
        }
      ],
      imageUrl: '',
      // 地图选择框是否可见
      mapDialogVisible: false,
      newmapDialogVisible: false,
      // 处于新增或修改状态
      projectStatus: 'update',
      projectInfoDetail: {},
      projectInfoDetailData: [],
      // 绑定外部项目弹框是否可见
      bindProjectDialogVisible: false,
      // 绑定外部项目选择的行
      choosedRow: {},
      // 新建项目步骤
      projectStep: 1,
      // 项目类型标准码
      projectTypeCode: [],
      projectType: {},
      // 建设性质标准码
      buildingPropertyCode: [],
      buildingPropertyMap: {},
      // 项目投资性质标准码
      investPropertyCode: [],
      investPropertyMap: {},
      // 视频安装状态标准码
      videoInstallCode: [],
      videoInstallMap: {},
      // 审批机构级别标准码
      auditLevelCode: [],
      auditLevelMap: {},
      // 项目状态标准码
      projectStateCode: [],
      projectStateMap: {},
      // 工程用途标准码
      projectUseageCode: [],
      projectUseageMap: {},
      orgTypeCode: [],
      // 结构类型标准码
      constructTypeCode: [],
      constructTypeMap: {},
      // 街道列表
      streetList: [],
      streetMap: {},
      //审批机构级别标准码
      approvalCode: [],
      approvaleMap: {},
      manufacturerList: [],
    }
  },
  watch: {//监听
    objRowRequest: {
      handler (n, o) {
        this.$emit('update:objRowRequest', n)
        // this.requestParams = this.objRowRequest;
        // this.requestParams = this.getDetailInfoNumChange(n);
        // console.log('w-子组件-objRowRequest-', n)
      },
      deep: true
    }
  },
  methods: {
    //下一步
    onstep (val) {
      if (val === 'first') {
        this.activeName = 'second'
      } else {
        this.activeName = 'third'
      }

    },
    // 返回触发后执行事件
    onBack () {
      window.history.back();//返回上一页
    },
    getBaseInfoParas () {//tab基本信息的传参
      let objParams = this.requestParams;
      return objParams;
    },
    //生成物料库
    getMaterial (id) {
      ProjectListApi.getMaterialStore({
        data: {
          project_id: id
        }
      }).then(res => {

      })
    },
    // 物料收发存同步项目
    syncProject (id) {
      ProjectListApi.syncProject({
        params: {
          projectId: id
        }
      }).then(res => { })
    },
    // 新增项目
    createProject () {
      ProjectListApi.saveProject({
        data: {
          ...this.getBaseInfoParas(),//基本信息
        }
      }).then(async res => {
        if (res.code === 0) {
          this.$message.success('新建项目成功')
          //新建项目生成物料库
          await this.getMaterial(res.data.project_id)
          await this.syncProject(res.data.project_id)
          window.localStorage.setItem('deptArr', JSON.stringify(this.deptArr))
          // this.$router.push('/ProjectList')
          window.localStorage.setItem('editProjectInfo', JSON.stringify({ id: res.data.project_id }))
          this.$router.push({ path: '/ProjectList' })

        }
      }, err => {
        console.log(err)
      })
    },
    // 编辑项目
    updateProject (projectId) {
      this.requestParams.project_id = projectId;//项目主键值
      ProjectListApi.editProject({
        data: this.requestParams
      }).then(res => {
        if (res.code === 0) {
          this.$message.success('修改项目成功')
          window.localStorage.setItem('deptArr', JSON.stringify(this.deptArr))
          this.$router.push({ path: '/ProjectList' })
          // window.localStorage.setItem('editProjectInfo', JSON.stringify(cb))
        }
      }, err => {
        console.log(err)
      })
    },
    // 判断是新增或是编辑
    editProject () {
      // this.$emit('update:step', 2)
      this.$refs.requestParams.validate((valid) => {
        if (valid) {
          let local = JSON.parse(window.localStorage.getItem('editProjectInfo') || '{}')
          let projectId = local.id || 0;
          let createUpdate = this.projectStatus === 'create' && projectId != 0;

          if (this.projectStatus === 'create' && projectId == 0) {
            if (this.requestParams.province != '' && this.requestParams.city != '' && this.requestParams.area != '') {
              this.createProject();//新增项目
            } else {
              this.$message.error('请选择省市区')
            }
          } else if (this.projectStatus === 'update' || createUpdate == true) {
            console.log('提交projectStatus=', this.projectStatus, 'createUpdate=', createUpdate, projectId)
            this.updateProject(projectId)
          }
        }
      })
    },

  },
  created () {
    // this.requestParams = this.objRowRequest;
    this.status = this.$route.query.status;
    this.projectStatus = this.status;
  }
}
